#!/bin/bash

# DEBUGGING
DEBUG=${DEBUG:-}

# LOGGER
LOGGER_LEVEL=${LOGGER_LEVEL:-info}

## CORE
REDMINE_ATTACHMENTS_DIR=${REDMINE_ATTACHMENTS_DIR:-$REDMINE_DATA_DIR/files}
REDMINE_PLUGINS_DIR=${REDMINE_PLUGINS_DIR:-$REDMINE_DATA_DIR/plugins}
REDMINE_THEMES_DIR=${REDMINE_THEMES_DIR:-$REDMINE_DATA_DIR/themes}
REDMINE_DOTFILES_DIR=${REDMINE_DOTFILES_DIR:-$REDMINE_DATA_DIR/dotfiles}
REDMINE_BACKUPS_DIR=${REDMINE_BACKUPS_DIR:-$REDMINE_DATA_DIR/backups}

REDMINE_HTTPS=${REDMINE_HTTPS:-false}
REDMINE_RELATIVE_URL_ROOT=${REDMINE_RELATIVE_URL_ROOT:-/}
REDMINE_PORT=${REDMINE_PORT:-}
if [[ $REDMINE_HTTPS == true ]]; then
  REDMINE_PORT=${REDMINE_PORT:-443}
else
  REDMINE_PORT=${REDMINE_PORT:-80}
fi

##
REDMINE_SECRET_TOKEN=${REDMINE_SECRET_TOKEN:-}
REDMINE_CONCURRENT_UPLOADS=${REDMINE_CONCURRENT_UPLOADS:-2}
REDMINE_SUDO_MODE_ENABLED=${REDMINE_SUDO_MODE_ENABLED:-false}
REDMINE_SUDO_MODE_TIMEOUT=${REDMINE_SUDO_MODE_TIMEOUT:-15}
REDMINE_FETCH_COMMITS=${REDMINE_FETCH_COMMITS:-disable}
REDMINE_AUTOLOGIN_COOKIE_NAME=${REDMINE_AUTOLOGIN_COOKIE_NAME:-autologin}
REDMINE_AUTOLOGIN_COOKIE_PATH=${REDMINE_AUTOLOGIN_COOKIE_PATH:-/}
if [[ $REDMINE_HTTPS == true ]]; then
    REDMINE_AUTOLOGIN_COOKIE_SECURE=${REDMINE_AUTOLOGIN_COOKIE_SECURE:-true}
else
    REDMINE_AUTOLOGIN_COOKIE_SECURE=${REDMINE_AUTOLOGIN_COOKIE_SECURE:-false}
fi

## BACKUPS
REDMINE_BACKUP_SCHEDULE=${REDMINE_BACKUP_SCHEDULE:-disable}
REDMINE_BACKUP_TIME=${REDMINE_BACKUP_TIME:-04:00}
REDMINE_BACKUP_EXPIRY=${REDMINE_BACKUP_EXPIRY:-}
case ${REDMINE_BACKUP_SCHEDULE} in
  daily|weekly|monthly) REDMINE_BACKUP_EXPIRY=${REDMINE_BACKUP_EXPIRY:-604800} ;;
  disable|*) REDMINE_BACKUP_EXPIRY=${REDMINE_BACKUP_EXPIRY:-0} ;;
esac

## DATABASE
DB_ADAPTER=${DB_ADAPTER:-}
DB_ENCODING=${DB_ENCODING:-}
DB_HOST=${DB_HOST:-}
DB_PORT=${DB_PORT:-}
DB_NAME=${DB_NAME:-}
DB_USER=${DB_USER:-}
DB_PASS=${DB_PASS:-}
DB_POOL=${DB_POOL:-5}
DB_SKIP_CHECK=${DB_SKIP_CHECK:-false}
DB_SSL_MODE=${DB_SSL_MODE:-}
DB_CREATE=${DB_CREATE:-true}

# backward compatibility
case ${DB_TYPE} in
  mysql) DB_ADAPTER=${DB_ADAPTER:-mysql2} ;;
  postgres) DB_ADAPTER=${DB_ADAPTER:-postgresql} ;;
  sqlite3) DB_ADAPTER=${DB_ADAPTER:-sqlite3} ;;
esac

## UNICORN (deprecated)
UNICORN_WORKERS=${UNICORN_WORKERS:-2}
UNICORN_TIMEOUT=${UNICORN_TIMEOUT:-60}

## PUMA
PUMA_WORKERS=${PUMA_WORKERS:-${UNICORN_WORKERS}}
# There is no PUMA timeout setting

## MEMCACHED
MEMCACHED_HOST=${MEMCACHED_HOST:-}
MEMCACHED_PORT=${MEMCACHED_PORT:-}

## SSL
SSL_CERTIFICATE_PATH=${SSL_CERTIFICATE_PATH:-$REDMINE_DATA_DIR/certs/redmine.crt}
SSL_KEY_PATH=${SSL_KEY_PATH:-$REDMINE_DATA_DIR/certs/redmine.key}
SSL_DHPARAM_PATH=${SSL_DHPARAM_PATH:-$REDMINE_DATA_DIR/certs/dhparam.pem}
SSL_CA_CERTIFICATES_PATH=${SSL_CA_CERTIFICATES_PATH:-$REDMINE_DATA_DIR/certs/ca.crt}
SSL_VERIFY_CLIENT=${SSL_VERIFY_CLIENT:-off}

## MAIL DELIVERY
SMTP_METHOD=${SMTP_METHOD:-smtp}
SMTP_DOMAIN=${SMTP_DOMAIN:-www.gmail.com}
SMTP_HOST=${SMTP_HOST:-smtp.gmail.com}
SMTP_PORT=${SMTP_PORT:-587}
SMTP_USER=${SMTP_USER:-}
SMTP_PASS=${SMTP_PASS:-}
SMTP_OPENSSL_VERIFY_MODE=${SMTP_OPENSSL_VERIFY_MODE:-}
SMTP_STARTTLS=${SMTP_STARTTLS:-true}
SMTP_TLS=${SMTP_TLS:-false}
SMTP_SSL=${SMTP_SSL:-false}
SMTP_CA_ENABLED=${SMTP_CA_ENABLED:-false}
SMTP_CA_PATH=${SMTP_CA_PATH:-$REDMINE_DATA_DIR/certs}
SMTP_CA_FILE=${SMTP_CA_FILE:-$REDMINE_DATA_DIR/certs/ca.crt}
if [[ -n ${SMTP_USER} ]]; then
  SMTP_ENABLED=${SMTP_ENABLED:-true}
  SMTP_AUTHENTICATION=${SMTP_AUTHENTICATION:-:login}
fi
SMTP_ENABLED=${SMTP_ENABLED:-false}

## INCOMING MAIL
IMAP_HOST=${IMAP_HOST:-imap.gmail.com}
IMAP_PORT=${IMAP_PORT:-993}
IMAP_USER=${IMAP_USER:-${SMTP_USER}}
IMAP_PASS=${IMAP_PASS:-${SMTP_PASS}}
IMAP_SSL=${IMAP_SSL:-true}
IMAP_STARTTLS=${IMAP_STARTTLS:-false}
IMAP_INTERVAL=${IMAP_INTERVAL:-30}
IMAP_ENABLED=${IMAP_ENABLED:-false}

## INCOMING MAIL (ADVANCED CONFIGURATION)
IMAP_FOLDER=${IMAP_FOLDER:-}
IMAP_MOVE_ON_SUCCESS=${IMAP_MOVE_ON_SUCCESS:-}
IMAP_MOVE_ON_FAILURE=${IMAP_MOVE_ON_FAILURE:-}
INCOMING_EMAIL_UNKNOWN_USER=${INCOMING_EMAIL_UNKNOWN_USER:-ignore}
INCOMING_EMAIL_NO_PERMISSION_CHECK=${INCOMING_EMAIL_NO_PERMISSION_CHECK:-false}
INCOMING_EMAIL_NO_ACCOUNT_NOTICE=${INCOMING_EMAIL_NO_ACCOUNT_NOTICE:-true}
INCOMING_EMAIL_DEFAULT_GROUP=${INCOMING_EMAIL_DEFAULT_GROUP:-}
INCOMING_EMAIL_PROJECT=${INCOMING_EMAIL_PROJECT:-}
INCOMING_EMAIL_STATUS=${INCOMING_EMAIL_STATUS:-}
INCOMING_EMAIL_TRACKER=${INCOMING_EMAIL_TRACKER:-}
INCOMING_EMAIL_CATEGORY=${INCOMING_EMAIL_CATEGORY:-}
INCOMING_EMAIL_PRIORITY=${INCOMING_EMAIL_PRIORITY:-}
INCOMING_EMAIL_PRIVATE=${INCOMING_EMAIL_PRIVATE:-}
INCOMING_EMAIL_ALLOW_OVERRIDE=${INCOMING_EMAIL_ALLOW_OVERRIDE:-}

## NGINX
NGINX_ENABLED=${NGINX_ENABLED:-true}
NGINX_WORKERS=${NGINX_WORKERS:-1}
NGINX_MAX_UPLOAD_SIZE=${NGINX_MAX_UPLOAD_SIZE:-20m}

NGINX_HSTS_ENABLED=${NGINX_HSTS_ENABLED:-$REDMINE_HTTPS_HSTS_ENABLED}
NGINX_HSTS_ENABLED=${NGINX_HSTS_ENABLED:-true}

NGINX_HSTS_MAXAGE=${NGINX_HSTS_MAXAGE:-$REDMINE_HTTPS_HSTS_MAXAGE}
NGINX_HSTS_MAXAGE=${NGINX_HSTS_MAXAGE:-31536000}

if [[ $REDMINE_HTTPS == true ]]; then
  NGINX_X_FORWARDED_PROTO=${NGINX_X_FORWARDED_PROTO:-https}
else
  NGINX_X_FORWARDED_PROTO=${NGINX_X_FORWARDED_PROTO:-\$scheme}
fi

## MINIMAGICK
REDMINE_MINIMAGICK_FONT_PATH=${REDMINE_MINIMAGICK_FONT_PATH:-/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf}

## AVATAR_SERVER
REDMINE_AVATAR_SERVER_URL=${REDMINE_AVATAR_SERVER_URL:-https://www.gravatar.com}